/************************************
*  Copyright (C) 2010 Julien MOTTIN.
*  Contact: julien.mottin@gueydan.eu
*
*  This file is part of Gueydan.
*
*  Gueydan is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  Gueydan is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with Gueydan.  If not, see <http://www.gnu.org/licenses/>.
*
*************************************/
// Local
#include "hopmodel.h"
#include "recipe.h"
#include "hop.h"
#include "weight.h"

// QtCore
#include <QModelIndex>
#include <QVariant>

HopModel::HopModel(Recipe *ipRecipe, QObject *parent)
    :QAbstractTableModel(parent),
    _pRecipe(ipRecipe)
{
    if (_pRecipe)
        _pRecipe->AddRef();
}

HopModel::~HopModel()
{
    if (_pRecipe)
        _pRecipe->Release();
    _pRecipe = NULL;
}

Qt::ItemFlags HopModel::flags(const QModelIndex &index) const
{
    // invalid item
    if (!index.isValid()) return Qt::NoItemFlags;

    // attributes other than quantity
    if (index.column()<2) return Qt::ItemIsSelectable;

    // attributes : quantity & unit
    return (Qt::ItemIsEditable | Qt::ItemIsSelectable);
}

QVariant HopModel::data(const QModelIndex &index, int role) const
{
    QVariant outItem;

    // Temporarly support only Display Role
    if (NULL!=_pRecipe && role==Qt::DisplayRole)
    {
        const Hop *zeHop = _pRecipe->GetHop(index.row());
        if (zeHop)
        {
            double val;
            QString unit;

            switch (index.column())
            {
            case 0:
                outItem = zeHop->_Name;
                break;
            case 1:
                outItem = zeHop->_Alpha;
                break;
            case 2:
                zeHop->_Amount.GUIValue(val,unit);
                outItem = val;
                break;
            case 3:
                zeHop->_Amount.GUIValue(val,unit);
                outItem = unit;
                break;
            }
            zeHop = NULL;
        }
    }

    return outItem;
}
